<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 求阶乘
        function fn(n) {
            if (n == 1) {
                return 1;
            }
            return n * fn(n - 1);
        }
        console.log(fn(3));

        // 递归求斐波那契数列，效率低
        function fb(n) {

            if (n == 1 || n == 2) {
                return 1;
            }
            return fb(n - 1) + fb(n - 2);
        }

        console.log(fb(4));

        // for循环求斐波那契数列
        var res = 1;
        var temp = 0;
        var pre = 1;
        function fc(n) {
            if (n == 1 || n == 2) {
                return 1;
            }

            for (var i = 3; i <= n; i++) {
                temp = res;
                res = pre + res;
                pre = temp;
            }
            return res;
        }
        console.log(fc(5));

    </script>
</body>

</html>